﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DotNetOpenAuth.OAuth.ChannelElements;

namespace APITwitter.Handlers
{
    public class SQLDBTokenManager : IConsumerTokenManager
    {
        #region Singleton
        private static SQLDBTokenManager _Current = new SQLDBTokenManager();
        public static SQLDBTokenManager Current
        {
            get
            {
                return _Current;
            }
        }

        private SQLDBTokenManager()
        {
        }
        #endregion

        #region IConsumerTokenManager 成员

        public string ConsumerKey
        {
            get { return Configuration.ConsumerKey; }
        }

        public string ConsumerSecret
        {
            get { return Configuration.ConsumerSecret; }
        }

        #endregion

        #region ITokenManager 成员

        public void ExpireRequestTokenAndStoreNewAccessToken(string consumerKey, string requestToken, string accessToken, string accessTokenSecret)
        {
            using (SQLDBStoreDataContext sdc = new SQLDBStoreDataContext(Configuration.ConnectionString))
            {

                try
                {
                    var ts = sdc.Twitter.Where(a => a.Token == requestToken || a.Token == accessToken);
                    sdc.Twitter.DeleteAllOnSubmit(ts);
                }
                finally
                {
                    sdc.Twitter.InsertOnSubmit(
                       new Twitter()
                       {
                           CreateDate = DateTime.Now,
                           Type = "Access",
                           Token = accessToken,
                           TokenSecret = accessTokenSecret,
                           ID = Guid.NewGuid(),
                           Specifier = string.Empty,
                           Password = string.Empty,
                       }
                   );
                    sdc.SubmitChanges();
                }

            }
        }

        public string GetTokenSecret(string token)
        {
            using (SQLDBStoreDataContext sdc = new SQLDBStoreDataContext(Configuration.ConnectionString))
            {
                return sdc.Twitter.First(t => t.Token == token).TokenSecret;
            }
        }

        public TokenType GetTokenType(string token)
        {
            throw new NotImplementedException();
        }

        public void StoreNewRequestToken(DotNetOpenAuth.OAuth.Messages.UnauthorizedTokenRequest request, DotNetOpenAuth.OAuth.Messages.ITokenSecretContainingMessage response)
        {
            using (SQLDBStoreDataContext sdc = new SQLDBStoreDataContext(Configuration.ConnectionString))
            {
                sdc.Twitter.InsertOnSubmit(
                    new Twitter()
                    {
                        ID = Guid.NewGuid(),
                        CreateDate = DateTime.Now,
                        Password = string.Empty,
                        Specifier = string.Empty,
                        Token = response.Token,
                        TokenSecret = response.TokenSecret,
                        Type = "Request",
                    }
                    );
                sdc.SubmitChanges();
            }
        }

        #endregion

        public void UpdateAccessTokeScreenNameAndPassword(string accessToken, string screenName, string password)
        {
            using (SQLDBStoreDataContext sdc = new SQLDBStoreDataContext(Configuration.ConnectionString))
            {
                Twitter t = sdc.Twitter.First(a => a.Token == accessToken);
                t.Specifier = screenName;
                t.Password = System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(password, "SHA1");
                sdc.SubmitChanges();
            }
        }

        public string GetAccessTokenByUsernameAndPassword(string userName, string password)
        {
            using (SQLDBStoreDataContext sdc = new SQLDBStoreDataContext(Configuration.ConnectionString))
            {
                Twitter t = sdc.Twitter.First(a => a.Specifier == userName && a.Password == System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(password, "SHA1"));
                return t.Token;
            }

        }

    }
}
